package com.tangoj.common.utils;

import com.tangoj.common.common.ErrorCode;
import com.tangoj.common.exception.BusinessException;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    public static <T> T parseParamDate(T t, Boolean judge) {
        try {
            Class clazz = t.getClass();
            Field startField = clazz.getDeclaredField("startTime");
            Field endField = clazz.getDeclaredField("endTime");

            // 设置可访问性为true
            startField.setAccessible(true);
            endField.setAccessible(true);

            if (startField == null || endField == null) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR);
            }
            String startDate = null;
            String endDate = null;
            if (judge) {
                startDate = DataFormatParseYMDStr((Date) startField.get(t));
                endDate = DataFormatParseYMDStr((Date) endField.get(t));
            } else {
                startDate = DataFormatParseHMSStr((Date) startField.get(t));
                endDate = DataFormatParseHMSStr((Date) endField.get(t));
            }

            String startName = startField.getName();
            String endName = startField.getName();

            // 获取 set 方法
            Method[] declaredMethods = clazz.getDeclaredMethods();

            for (Method declaredMethod : declaredMethods) {
                String setMethodNameStart = getSetMethodName(startName);
                String setMethodNameEnd = getSetMethodName(endName);
                if (setMethodNameStart.equals(declaredMethod.getName())) {
                    declaredMethod.invoke(t, startDate);
                }
                if (setMethodNameEnd.equals(declaredMethod.getName())) {
                    declaredMethod.invoke(t, endDate);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    private static String getSetMethodName(String name) {
        return "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public static Date DataFormatParseYMD(Date date) {
        // 定义要转换的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 将Date类型转换为字符串
        String dateString = sdf.format(date);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String DataFormatParseYMDStr(Date date) {
        // 定义要转换的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static Date DataFormatParseHMS(Date date) {
        // 定义要转换的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将Date类型转换为字符串
        String dateString = sdf.format(date);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String DataFormatParseHMSStr(Date date) {
        // 定义要转换的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.format(date);
    }
}
